package com.zslc.foreignoption.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统一返回类型
 */

public class ResultCMS {

    private String success;
    private int count;
    private Object data;

    /**
     * 成功：无业务数据
     *
     * @return
     */
    public static ResultCMS success() {
        return new ResultCMS("true", new HashMap<>(0));
    }

    /**
     * 成功：自定义data结构
     *
     * @return
     */
    public static ResultCMS success(Object data) {
        return new ResultCMS("true", data);
    }

    /**
     * 成功：自定义响应data:{"key":"value"}
     *
     * @return
     */
    public static ResultCMS success(String key, Object value) {
        Map map = new HashMap<>();
        map.put(key, value);
        return new ResultCMS("true", map);
    }

    /**
     * 成功：响应data:{"key":"value","key1":"value1",...}
     *
     * @param map
     * @return
     */
    public static ResultCMS success(Map map) {
        return new ResultCMS("true", map);
    }

    /**
     * 成功：响应data:[]
     *
     * @return
     */
    public static ResultCMS success(List list) {
        if (list!=null) {
            return new ResultCMS("true", list.size(), list);
        }else {
            return new ResultCMS("true", 0, new ArrayList<>());
        }
    }

    /**
     * 成功：
     * 调用示例：ResultCMS.successArr("value1", 2, "value3"...)
     * 响应data:["value1", 2, "value3"]
     *
     * @return
     */
    public static ResultCMS successArr(Object... values) {
        return new ResultCMS("true", values);
    }

    /**
     * 成功：响应业务数据,Map或List或实体...自定义进行传递
     *
     * @return
     */
    public static ResultCMS successObject(Object data) {
        return new ResultCMS("true", data);
    }

    /**
     * 失败：
     *
     * @param errorCode 错误码
     * @param msg       错误信息
     * @return
     */
    public static ResultCMS fail(int errorCode, String msg) {
        Map map = new HashMap<>();
        map.put("error", errorCode);
        map.put("msg", msg);
        return new ResultCMS("false", map);
    }

    /**
     * 响应信息：未知的服务器内部错误
     *
     * @return
     */
    public static ResultCMS fail_5000() {
        Map map = new HashMap<>();
        map.put("error", ResultConstants.code_5000.getCode());
        map.put("msg", ResultConstants.code_5000.getMsg());
        return new ResultCMS("false", map);
    }

    /**
     * 响应信息：未知的服务器内部错误
     *
     * @return
     */
    public static ResultCMS fail_5101() {
        Map map = new HashMap<>();
        map.put("error", ResultConstants.code_5101.getCode());
        map.put("msg", ResultConstants.code_5101.getMsg());
        return new ResultCMS("false", map);
    }


    public ResultCMS() {
    }

    public ResultCMS(String success, Object data) {
        this.success = success;
        this.data = data;
    }

    public ResultCMS(String success, int count, Object data) {
        this.success = success;
        this.count = count;
        this.data = data;
    }

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public int getCount() {
        return count;
    }

    public ResultCMS setCount(int count) {
        this.count = count;
        return this;
    }

    @Override
    public String toString() {
        return "ResultCMS{" +
                "success=" + success +
                ", data=" + data +
                '}';
    }
}
